package com.ultrapower.ctool.xn_app;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ultrapower.ctool.ConfUtils;
import com.ultrapower.ctool.bean.WangGeBean;
import com.ultrapower.ctool.core.CustomApplication;
import com.ultrapower.ctool.http.BaseActivity;
import com.ultrapower.ctool.model.RegisterRolePirVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddXunChaResultActivity extends BaseActivity implements View.OnClickListener {
    private EditText bh_add_xc_result;//巡查表编号
    private Spinner xcname_add_xc_result;//项目名称
    private Spinner xcresult_add_xc_result;//巡查项结果
    private EditText detail_add_xc_result;//结果描述
    private Button save_add_xc_result;
    private Button cancle_add_xc_result;
    private Gson gson = new Gson();
    private CustomApplication application;

    private ArrayAdapter<RegisterRolePirVO> mnameAdapter;
    private ArrayAdapter<RegisterRolePirVO> mresultAdapter;

    private static List<RegisterRolePirVO> mnames =new ArrayList<RegisterRolePirVO>();
    private  static  List<RegisterRolePirVO> mresults=new ArrayList<RegisterRolePirVO>();
    static {
        mresults.add(new RegisterRolePirVO("",""));
        mresults.add(new RegisterRolePirVO("1","达标"));
        mresults.add(new RegisterRolePirVO("2","不达标"));
        mnames.add(new RegisterRolePirVO("",""));
        mnames.add(new RegisterRolePirVO("1","燃气安全"));
        mnames.add(new RegisterRolePirVO("2","电源安全"));
    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Toast.makeText(getApplicationContext(), "添加成功", Toast.LENGTH_SHORT).show();
//                    Intent intent = new Intent(AddXunChaResultActivity.this, null);
//                    startActivity(intent);
                    finish();
                    break;
                case 0:
                    Toast.makeText(getApplicationContext(), "系统出现异常", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    private AdapterView.OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
            ((TextView) parent.getChildAt(0)).setTextSize(18);
        }

        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_xun_cha_result);
        application = (CustomApplication) getApplication();

        init();
    }

    protected void init(){
        bh_add_xc_result = (EditText)findViewById(R.id.bh_add_xc_result);
        xcname_add_xc_result = (Spinner)findViewById(R.id.xcname_add_xc_result);
        xcresult_add_xc_result = (Spinner)findViewById(R.id.xcresult_add_xc_result);
        detail_add_xc_result = (EditText)findViewById(R.id.detail_add_xc_result);
        save_add_xc_result = (Button)findViewById(R.id.save_add_xc_result);
        cancle_add_xc_result = (Button)findViewById(R.id.cancle_add_xc_result);
        save_add_xc_result.setOnClickListener(this);
        cancle_add_xc_result.setOnClickListener(this);
        // TODO: 2017/5/21 编号
        Intent intent = getIntent();
        String cid = intent.getStringExtra("cid");
        bh_add_xc_result.setText(cid);
        //将可选内容与ArrayAdapter连接起来
        mnameAdapter = new ArrayAdapter<RegisterRolePirVO>(this, android.R.layout.simple_spinner_item, mnames);
        //设置下拉列表的风格
        mnameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter 添加到spinner中
        xcname_add_xc_result.setAdapter(mnameAdapter);
        //添加事件Spinner事件监听
        xcname_add_xc_result.setOnItemSelectedListener(OnCatSpinnerCL);
        //设置默认值
        xcname_add_xc_result.setVisibility(View.VISIBLE);

        //将可选内容与ArrayAdapter连接起来
        mresultAdapter = new ArrayAdapter<RegisterRolePirVO>(this, android.R.layout.simple_spinner_item, mresults);
        //设置下拉列表的风格
        mresultAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter 添加到spinner中
        xcresult_add_xc_result.setAdapter(mresultAdapter);
        //添加事件Spinner事件监听
        xcresult_add_xc_result.setOnItemSelectedListener(OnCatSpinnerCL);
        //设置默认值
        xcresult_add_xc_result.setVisibility(View.VISIBLE);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save_add_xc_result:
                if ("".equals(((RegisterRolePirVO) xcname_add_xc_result.getSelectedItem()).getId())
                        || "".equals(bh_add_xc_result.getText().toString().trim())
                        ) {
                    Toast.makeText(getApplicationContext(), "请填写完整信息", Toast.LENGTH_SHORT).show();
                } else {
                    final Map<String, String> map = new HashMap<String, String>();
                    //WangGeBean bean = new WangGeBean();
                    map.put("cid",bh_add_xc_result.getText().toString());
                    map.put("mname", ((RegisterRolePirVO) xcname_add_xc_result.getSelectedItem()).getId());
                    map.put("itemvalue", ((RegisterRolePirVO) xcresult_add_xc_result.getSelectedItem()).getId());
                    map.put("mcaption", detail_add_xc_result.getText().toString().trim());
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                OkHttpClient client = new OkHttpClient();
                                FormBody.Builder builder = new FormBody.Builder()
                                        // TODO: 2017/5/21 String.valueOf(application.getLoginUser().getLogin_id())
                                        .add("userId", String.valueOf(application.getLoginUser().getLogin_id()));
                                for (Map.Entry<String, String> m : map.entrySet()) {
                                    builder.add(m.getKey(), m.getValue());
                                }
                                RequestBody resultRequestBody = builder.build();
                                Request enttypeRequest = new Request.Builder().url(ConfUtils.SERVICE_URL + "form/doAdd/c_aj_xcxm").post(resultRequestBody).build();
                                Response enttyperesponse = client.newCall(enttypeRequest).execute();
                                if (enttyperesponse.isSuccessful()) {
                                    String result = enttyperesponse.body().string();
                                    Map<String, Object> resultMap = gson.fromJson(result, Map.class);
                                    System.out.println(resultMap);
                                    if ((boolean) resultMap.get("success")) {

                                        Message message = new Message();
                                        message.what = 1;
                                        handler.sendMessage(message);


                                    } else {
                                        Message message = new Message();
                                        message.what = 0;
                                        handler.sendMessage(message);
                                    }
                                }
                            } catch (Exception e) {
                                Message message = new Message();
                                message.what = 0;
                                handler.sendMessage(message);
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }

                break;
            case R.id.cancle_add_xc_result:
                finish();
                break;
        }
    }
}
